package plugin.bpce.modelo;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import plugin.bpce.Object.RepositoryObject;

public class BPCEResult implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 0L;
	private String Path, NomeDiagrama, NomeAutor, PtsDiagrama, AgencyID;    
	private byte[] Imagem, Diagrama;
	
	public BPCEResult(String Path, RepositoryObject ro){
		this.Path = Path;		
		
		CarregaZip(ro);
	}
	
	public void CarregaZip(RepositoryObject ro){
		ZipFile zip;		
		ZipEntry zipEntry;
		InputStream inEntry;
		BufferedInputStream buffer;		
		
		try{
			zip = new ZipFile(this.Path);
			
			zipEntry = zip.getEntry("MO.xml");
			inEntry = zip.getInputStream(zipEntry);
			AbreMO(inEntry, ro);
			
			zipEntry = zip.getEntry("Imagem.jpg");
			inEntry = zip.getInputStream(zipEntry);
			buffer = new BufferedInputStream(inEntry);				
			this.Imagem = new byte[buffer.available()];
			buffer.read(this.Imagem);
			
			zipEntry = zip.getEntry("Modelo.xml");
			inEntry = zip.getInputStream(zipEntry);
			buffer = new BufferedInputStream(inEntry);
			this.Diagrama = new byte[buffer.available()];
			buffer.read(this.Diagrama);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void AbreMO(InputStream in, RepositoryObject ro){
		DocumentBuilder Build;
		DocumentBuilderFactory Factory ;		
		Node noNomeModelo, noNomeAutor;		
	    Document ModelObject;	    	    
	    XPath xpath;
		
		try{
		    Factory = DocumentBuilderFactory.newInstance();
		    Factory.setNamespaceAware(false);
		    Factory.setIgnoringComments(true);		            
		    Build = Factory.newDocumentBuilder();
		    xpath = XPathFactory.newInstance().newXPath();
	        
		    ModelObject = Build.parse(in);
		    noNomeAutor = (Node) xpath.evaluate("//NomeAutor", ModelObject, XPathConstants.NODE);
		    noNomeModelo = (Node) xpath.evaluate("//NomeDiagrama", ModelObject, XPathConstants.NODE);
		    
		    this.NomeDiagrama = noNomeModelo.getTextContent();
		    this.NomeAutor = noNomeAutor.getTextContent();
		    this.PtsDiagrama = Float.toString(ro.getPontuacaoDiagrama(this.NomeDiagrama, this.NomeAutor));	
		}
		catch(Exception e){
			e.printStackTrace();			
		}		
	}
	
	public String getNomeDiagrama(){
		return this.NomeDiagrama;
	}
	
	public String getPtsDiagrama(){
		return this.PtsDiagrama;
	}
	
	public String getNomeAutor(){
		return this.NomeAutor;
	}
	
	public String getAgencyId(){
		return this.AgencyID;
	}
	
	public void setAgencyId(String AgencyId){
		this.AgencyID = AgencyId;
	}
	
	public byte[] getImagem(){
		return this.Imagem;
	}

	public byte[] getDiagrama(){
		return this.Diagrama;
	}
}